<?php

namespace app\beautiful\model\order;


use app\common\model\CommonModel;



class OrderGoodsGroupDetailModel extends CommonModel{

    protected  $name = 'beautiful_order_goods_group_detail';

    protected  $pk   = 'order_goods_group_detail_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }

    public static function list($where,$order='',$page=0,$limit=0,$field='*'){
        $query = self::where($where)->field($field);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        return compact('count','list');
    }
    public static function validList($where){
        $group_detail = self::where($where)->select()->toArray();
        $goods_write_off_model = new GoodsWriteOffModel();
        foreach ($group_detail as $k=>$v){
            if($v['use_condition']!=1) $is_many_write_off = 1;
            $num += $v['num'];
            $remain_num += $v['remain_num'];
            if($v['use_condition']!=1 && $v['num']>2 && $v['remain_num']>0){
                //核销数据
                $hx = GoodsWriteOffModel::where(['order_goods_group_detail_id'=>$v['order_goods_group_detail_id'],'goods_id'=>$v['goods_id']])->order(['add_time'=>'asc'])->select()->toArray();
                $v['is_effect'] = 1;
                //套餐详情有效期
                if($v['end_type']==1 && $hx){
                    $first_time = strtotime($hx[0]['add_time']);
                    if($v['end_time_type'] == 1){
                        $v['start_time'] = date('Y-m-d H:i:s',$datas->pay_time);
                        $v['end_time'] = date("Y-m-d",$first_time + $v['end_time_num'] * 86400)." 23:59:59";
                    }else{
                        $v['start_time'] = date('Y-m-d H:i:s',$datas->pay_time);
                        $v['end_time'] = GetMonth($first_time,$v['end_time_num']);

                    }
                    if(time()<strtotime($v['start_time']) && time()>strtotime($v['end_time'])){
                        $v['is_effect'] = 0;
                    }
                }
                $v['write_off'] = $hx;
                $write_off[] = $v;
            }
        }
    }
}

